local sk__jueshi = fk.CreateSkill {

  name = "sk__jueshi",

  tags = { Skill.Compulsory, },

}



sk__jueshi:addEffect(fk.BeforeMaxHpChanged, {
  name = "sk__jueshi",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = { fk.BeforeMaxHpChanged, fk.EnterDying },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__jueshi.name)
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.BeforeMaxHpChanged then
      data:preventMaxHpChange()
    else
      local room = player.room
      while not player.dead and player.hp < 1 do
        local cards = table.filter(room.draw_pile, function(id)
          return Fk:getCardById(id).trueName == "peach" or Fk:getCardById(id).trueName == "analeptic"
        end)
        for _, p in ipairs(room:getAllPlayers()) do
          table.insertTable(cards, table.filter(p:getCardIds("h"), function(id)
            return Fk:getCardById(id).trueName == "peach" or Fk:getCardById(id).trueName == "analeptic"
          end))
        end
        if #cards == 0 then break end
        local card = Fk:getCardById(table.random(cards))
        if card.trueName == "peach" then
          room:useCard {
            from = player,
            tos = { player },
            card = card,
            extraUse = true,
          }
        elseif card.trueName == "analeptic" then
          room:useCard({
            from = target,
            tos = { target },
            card = card,
            extra_data = { analepticRecover = true },
            extraUse = true,
          })
        end
      end
    end
  end,
})
sk__jueshi:addEffect(fk.EnterDying, {
  name = "sk__jueshi",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = { fk.BeforeMaxHpChanged, fk.EnterDying },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__jueshi.name)
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.BeforeMaxHpChanged then
      return true
    else
      local room = player.room
      while not player.dead and player.hp < 1 do
        local cards = table.filter(room.draw_pile, function(id)
          return Fk:getCardById(id).trueName == "peach" or Fk:getCardById(id).trueName == "analeptic"
        end)
        for _, p in ipairs(room:getAllPlayers()) do
          table.insertTable(cards, table.filter(p:getCardIds("h"), function(id)
            return Fk:getCardById(id).trueName == "peach" or Fk:getCardById(id).trueName == "analeptic"
          end))
        end
        if #cards == 0 then break end
        local card = Fk:getCardById(table.random(cards))
        if card.trueName == "peach" then
          room:useCard {
            from = player,
            tos = { player },
            card = card,
            extraUse = true,
          }
        elseif card.trueName == "analeptic" then
          room:useCard({
            from = target,
            tos = { target },
            card = card,
            extra_data = { analepticRecover = true },
            extraUse = true,
          })
        end
      end
    end
  end,
})
sk__jueshi:addEffect(fk.GameStart, {
  refresh_events = { fk.GameStart, fk.EventAcquireSkill },
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill("sk__jueshi", true) and player.maxHp ~= 1
  end,
  on_refresh = function(self, event, target, player, data)
    player.maxHp = 1
    player.hp = math.min(player.hp, 1)
    player.room:broadcastProperty(player, "maxHp")
    player.room:broadcastProperty(player, "hp")
  end,
})
sk__jueshi:addEffect(fk.EventAcquireSkill, {
  refresh_events = { fk.GameStart, fk.EventAcquireSkill },
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill("sk__jueshi", true) and player.maxHp ~= 1
  end,
  on_refresh = function(self, event, target, player, data)
    player.maxHp = 1
    player.hp = math.min(player.hp, 1)
    player.room:broadcastProperty(player, "maxHp")
    player.room:broadcastProperty(player, "hp")
  end,
})

return sk__jueshi
